package com.vitabuybuy.tv.ui.auth

import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import androidx.core.os.bundleOf

import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityBindEmailBinding
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.ext.mmkv
import com.library.common.ext.setOnclickNoRepeat
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.auth.viewmodel.AuthViewModel
import com.library.common.util.MMKVContancts
import kotlinx.android.synthetic.main.activity_bind_email.*

class VerifyEmailActivity : BaseActivity<AuthViewModel, ActivityBindEmailBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_bind_email

    private var isGetCode = false

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.bind_email_1))
        etEmail.setText(mmkv.decodeString(MMKVContancts.USER_EMAIL, ""))
        etEmail.isEnabled = false
    }

    override fun eventHandling() {
        setOnclickNoRepeat(tvGetCode, btnFinish){v->
            when (v) {
                tvGetCode -> {
                    viewModel.sendToEmail(etEmail.text.toString())
                }
                btnFinish -> {
                    if (!isGetCode) {
                        XToast.normal(getString(R.string.please_get_code))
                        return@setOnclickNoRepeat
                    }
                    if (etCode.text.isEmpty()) {
                        XToast.normal(getString(R.string.enter_code))
                        return@setOnclickNoRepeat
                    }
                    viewModel.validateByEmail(etEmail.text.toString(), etCode.text.toString())
                }
            }
        }
    }


    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            4 -> {
                XToast.success(getString(R.string.already_sms_code))
                timer.start()
            }
            5 -> {
                BindEmailActivity.openActivity(
                    mContext,
                    bundleOf(Pair("from", BindEmailActivity.FROM_BIND_EMAIL))
                )
                finish()
            }
        }
    }

    private val timer = object : CountDownTimer((60 * 1000).toLong(), 1000) {
        override fun onTick(millisUntilFinished: Long) {
            isGetCode = true
            mBinding.tvGetCode.text = String.format(
                getString(R.string.get_sms_code_s),
                millisUntilFinished / 1000
            )
            mBinding.tvGetCode.isEnabled = false
        }

        override fun onFinish() {
            mBinding.tvGetCode.text = getString(R.string.get_sms_code)
            mBinding.tvGetCode.isEnabled = true
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, VerifyEmailActivity::class.java)
        }
    }
}
